﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Collections;
using System.Reflection;
using UMC.Web.UI;
using UMC.Web;
using UMC.Wiki.Entities;

namespace UMC.Wiki.Activities
{
    class SubjectUIMinActivity : WebActivity
    {
        public override void ProcessActivity(WebRequest request, WebResponse response)
        {

            var strId = this.AsyncDialog("Id", g =>
             {
                 return new Web.UITextDialog() { Title = "主题" };
             });
            var form = request.SendValues ?? new UMC.Web.WebMeta();

            if (form.ContainsKey("limit") == false)
            {
                this.Context.Send(new UISectionBuilder(request.Model, request.Command, new UMC.Web.WebMeta().Put("Id", strId))
                 .CloseEvent("Subject.Portfolio.Change").Builder(), true);
            }
            var ui = UISection.Create();
            Subject sub;
            var sid = UMC.Data.Utility.UInt64Parse(strId);
            if (sid.HasValue)
            {
                sub = DataFactory.Instance().Subject(sid.Value);
            }
            else
            {
                var codes = new List<String>(strId.Split('/'));
                switch (codes.Count)
                {
                    case 1:
                        codes.Insert(0, "Help");
                        codes.Insert(0, "UMC");
                        break;
                    case 2:
                        codes.Insert(0, "UMC");
                        break;
                    case 3:
                        break;
                    default:
                        ui.Add("Desc", new UMC.Web.WebMeta().Put("desc", "不正确的编码").Put("icon", "\uea0d")
         , new UMC.Web.WebMeta().Put("desc", "{icon}\n{desc}"), new UIStyle().Align(1).Color(0xaaa).Padding(20, 20).BgColor(0xfff).Size(12).Name("icon", new UIStyle().Font("wdk").Size(60)));

                        response.Redirect(ui);
                        break;
                }

                var team = DataFactory.Instance().Project(codes[0]);
                if (team == null)
                {
                    ui.Add("Desc", new UMC.Web.WebMeta().Put("desc", String.Format("不存在“{0}”此项目", codes[0])).Put("icon", "\uea0d")
     , new UMC.Web.WebMeta().Put("desc", "{icon}\n{desc}"), new UIStyle().Align(1).Color(0xaaa).Padding(20, 20).BgColor(0xfff).Size(12).Name("icon", new UIStyle().Font("wdk").Size(60)));

                    response.Redirect(ui);
                }
                var projectItem = DataFactory.Instance().ProjectItem(team.Id.Value, codes[0]);

                if (projectItem == null)
                {
                    ui.Add("Desc", new UMC.Web.WebMeta().Put("desc", String.Format("不存在“{0}/{1}”此栏位", team.Code, codes[1])).Put("icon", "\uea0d")
     , new UMC.Web.WebMeta().Put("desc", "{icon}\n{desc}"), new UIStyle().Align(1).Color(0xaaa).Padding(20, 20).BgColor(0xfff).Size(12).Name("icon", new UIStyle().Font("wdk").Size(60)));

                    response.Redirect(ui);
                }
                sub = DataFactory.Instance().Subject(team.Id.Value, projectItem.Id.Value, codes[2]);
            }


            int limit = UMC.Data.Utility.IntParse(form["limit"] as string, 25);

            int start = UMC.Data.Utility.IntParse(form["start"] as string, 0);


            if (sub == null)
            {

                if (strId.IndexOf("/") > 0)
                {
                    ui.Add("Desc", new UMC.Web.WebMeta().Put("desc", String.Format("未有{0}路径文档", strId)).Put("icon", "\uea0d")
                        , new UMC.Web.WebMeta().Put("desc", "{icon}\n{desc}"), new UIStyle().Align(1).Color(0xaaa).Padding(20, 20).BgColor(0xfff).Size(12).Name("icon", new UIStyle().Font("wdk").Size(60)));
                }
                else
                {
                    ui.Add("Desc", new UMC.Web.WebMeta().Put("desc", "此图文已删除").Put("icon", "\uea0d")
                        , new UMC.Web.WebMeta().Put("desc", "{icon}\n{desc}"), new UIStyle().Align(1).Color(0xaaa).Padding(20, 20).BgColor(0xfff).Size(12).Name("icon", new UIStyle().Font("wdk").Size(60)));
                }
                response.Redirect(ui);
            }
            else if (sub.Status == -2)
            {

                var member = DataFactory.Instance().ProjectMember(sub.project_id.Value, Context.Token.UserId.Value);
                if (member != null)
                {
                    switch (member.AuthType)
                    {
                        case WebAuthType.Admin:
                        case WebAuthType.User:
                            break;
                        default:
                            ui.Add("Desc", new UMC.Web.WebMeta().Put("desc", "此图文已删除").Put("icon", "\uea0d")
               , new UMC.Web.WebMeta().Put("desc", "{icon}\n{desc}"), new UIStyle().Align(1).Color(0xaaa).Padding(20, 20).BgColor(0xfff).Size(12).Name("icon", new UIStyle().Font("wdk").Size(60)));

                            response.Redirect(ui);
                            break;
                    }

                }
                else
                {
                    ui.Add("Desc", new UMC.Web.WebMeta().Put("desc", "此图文已删除").Put("icon", "\uea0d")
       , new UMC.Web.WebMeta().Put("desc", "{icon}\n{desc}"), new UIStyle().Align(1).Color(0xaaa).Padding(20, 20).BgColor(0xfff).Size(12).Name("icon", new UIStyle().Font("wdk").Size(60)));

                    response.Redirect(ui);
                }

                ui.Title = new UITitle("回收站");


                ui.UIFootBar = new UIFootBar().AddText(
                new UIEventText("还原").Click(new UIClick(request.Model, "Recycle", "Id", sub.Id.ToString(), "Type", "Restore")
                 ).Style(new UIStyle().BgColor()), new UIEventText("删除").Click(new UIClick(request.Model, "Recycle", "Id", sub.Id.ToString(), "Type", "Del")).Style(new UIStyle().Color(0xfff).BgColor(0xff4949)));
                ui.UIFootBar.IsFixed = true;
            }
        else
            {
                ui.Title = new UITitle(String.Empty);
                ui.Title.Name("title", sub.Title);
            }

            var desc = new UIDesc(sub.Title);
            desc.Style.Bold().Size(18).Name("border", "none");
            if (request.IsApp)
            {
                desc.Style.Padding(55, 10, 10, 10);
            }
            else
            {
                desc.Style.Padding(10);

            }
            ui.Add(desc);

            var content = DataFactory.Instance().Content(sid.Value) ?? new SubjectContent();

            ui.DisableSeparatorLine();
            ui.AddCells(Utility.ContentCells(content)); ;

            response.Redirect(ui);
        }

    }
}